package com.kang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kang.Vo.NoticeVo;
import com.kang.bean.Notice;
import com.kang.bean.Users;
import com.kang.mapper.NoticeMapper;
import com.kang.service.NoticeService;
import com.kang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: photo_fawu
 * @description:
 * @author: K.faWu
 * @create: 2021-11-19 14:20
 **/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private UserService userService;

    @Override
    public List<NoticeVo> getNew() {
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        PageHelper.startPage(1,5);
        return noticeMapper.getNew(time);
    }

    @Override
    public void addNotice(Notice notice, HttpServletRequest request) {
        if (notice.getUserId() == null){
            Users users = userService.getUsers(request);
            notice.setUserId(users.getId());
        }
        noticeMapper.insert(notice);
    }

    @Override
    public PageInfo<NoticeVo> getList(NoticeVo noticeVo, Pageable page) {
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<NoticeVo> list = noticeMapper.getList(noticeVo.getTitle(), noticeVo.getAdminName(), noticeVo.getTime());
        return new PageInfo<>(list);
    }
}
